$(function(){
    q={
        pagenum:'1',
        pagesize:'2',
        cate_id:'',
        state:''
    }
    
    initArtCateList()
   function initArtCateList(){
        axios({
            url: '/my/article/cates',
            method: 'get'
        }).then(({ data: res }) => {
            if (res.status !== 0) {
                return layer.msg(res.message)
            }
            let arr = ['<option value="">请选择文章类别</option>']
            res.data.forEach(value => {
                arr.push(`
             <option value="${value.Id}">${value.name}</option>
          `)
            })
            $('[name="cate_id"]').html(arr.join(''))
            layui.form.render()
        })
    }

    $('#searchForm').on('submit',function(e){
      e.preventDefault()
        q.cate_id = $('[name="cate_id"]').val()
        q.state = $('[name="state"]').val()
        initArticleList()
    })

    initArticleList()

    function initArticleList(){
          axios({
              url:'/my/article/list',
              method:'get',
              params: q
          }).then(({data:res})=>{
             if(res.status!==0){
                 return layer.msg(res.message)
             }
             let htmlArr=[]
             res.data.forEach(value => {
                 htmlArr.push(`
                   <tr>
                        <td>${value.title}</td>
                        <td>${value.cate_name}</td>
                        <td>${formatDate(value.pub_date)}</td>
                        <td>${value.state}</td>
                        <td><button type="button" class="layui-btn layui-btn-xs">编辑</button>
                            <button type="button" class="layui-btn layui-btn-xs layui-btn-danger del" data-id="${value.Id}">删除</button></td>
                    </tr>
                 `)
             })

             $('tbody').empty().html(htmlArr.join(''))
              renderPage(res.total)
          })
    }

    function renderPage(total){
        layui.laypage.render({
            elem: 'Page' 
            , count: total 
            , limit:q.pagesize
            , curr:q.pagenum
            , limits:[2,5,10,15,20]
            , layout: ['count', 'limit', 'prev', 'page', 'next','skip']
            , jump: function (obj, first) {

                if (!first) {
                    q.pagenum=obj.curr
                    q.pagesize=obj.limit
                    initArticleList()
                }
            }
        })
    }

    $('body').on('click','.del',function(){
        let id=$(this).attr('data-id')
        layer.confirm('确认删除吗?', { icon: 3, title: '提示' }, function (index) {
            axios({
                url: `/my/article/delete/${id}`,
                method: 'get'
            }).then(({ data: res }) => {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                layer.msg(res.message)
                $('.del').length == 1 && q.pagenum >= 2 && q.pagenum--
                initArticleList()
            })
            layer.close(index)
        })
        
    })
})